package com.hefu.virtuallife.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.hefu.virtuallife.R;
import com.hefu.virtuallife.utils.UiCompat;

import java.util.ArrayList;
import java.util.List;


/**
 * creat_user: zhengzaihong
 * Email:1096877329@qq.com
 * creat_date: 2018/7/25
 * creat_time: 9:02
 * describe 自定义饼状图
 **/
public class RoundChatView extends View {

    private Context mContext;
    /**
     * 控件的宽度
     */
    private int mWidth;

    /**
     * 圆环的宽度
     */
    private int mStrokeWidth;

    /**
     * 画笔
     */
    private Paint mPaint;
    /**
     * 一共多少份
     */
    private int mCount = 0;
    /**
     * 每份所占的比例
     */
    private List<Float> mList = new ArrayList<Float>();
    /**
     * 饼状图每块的颜色
     */
    private int[] mColor = {UiCompat.getColor(getResources(), R.color.chat_red),
            UiCompat.getColor(getResources(), R.color.chat_blue), UiCompat.getColor(getResources(), R.color.chat_orange),
            UiCompat.getColor(getResources(), R.color.chat_green)};
    /**
     * 存储没份所占的角度
     */
    private List<Float> mDegree = new ArrayList<>();

    public RoundChatView(Context context) {
        this(context, null);
    }

    public RoundChatView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundChatView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // 获取自定义的属性
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundChatView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.RoundChatView_strokeWidth:  // 圆环宽度
                    mStrokeWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;

            }

        }
    }

    private void initData(Context context) {
        // 计算角度
        if (mList.size() > 0) {
            for (int i = 0; i < mList.size(); i++) {
                Float ratio = mList.get(i);
                Float degree = 360 * ratio;
                mDegree.add(degree);
            }
        }
    }

    /**
     * 设置份数和所占比例
     *
     * @param count
     * @param list
     */
    public void setCount(int count, List<Float> list) {
        this.mCount = count;
        this.mList = list;
        initData(mContext);
    }

    private int lastDegree = 0;  //已经画过的角度

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();

        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth); // 设置圆环的宽度

        int centre = mWidth / 2; // 获取圆心的x坐标
        int radius = centre - mStrokeWidth / 2;// 半径
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

        // 画圆环
        for (int i = 0; i < mCount; i++) {
            float degree = mDegree.get(i);
            Log.i("aijie", "角度==" + degree);
            lastDegree += degree;
            mPaint.setColor(i > mColor.length ? mColor[i - mColor.length] : mColor[i]);  // 循环取颜色值
            canvas.drawArc(oval, -90 - lastDegree, degree, false, mPaint); // 根据角度画圆弧
        }
    }
}

